<script setup lang="ts">
import { ref, reactive } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
import type { PostLoginPayload } from '@/api/user/user'
import type { FormRules } from 'element-plus'
import useUserStore from '@/stores/user'
import { useRouter } from 'vue-router'
const user = useUserStore()
const router = useRouter()

const loginForm = ref<PostLoginPayload>({
  username: '神龙教主',
  password: 'admin888',
})
const loginFormRules = reactive<FormRules<PostLoginPayload>>({
  username: [{ required: true, message: '请输入用户名称', trigger: 'blur' }],
  password: [
    { required: true, message: '请输入用户密码', trigger: 'blur' },
    {
      min: 6,
      max: 16,
      message: '用户密码在6~16位之间',
      trigger: 'blur',
    },
  ],
})
const loginFormRef = ref()
// 登录方法
const handleLogin = () => {
  // 兜底校验
  loginFormRef.value.validate(async (valid: any) => {
    if (!valid) return
    await user.login(loginForm.value)
    router.push('/')
  })
}
</script>

<template>
  <div class="login-container">
    <div class="loginBox">
      <div class="left">
        <img src="@/assets/images/loginBg.png" alt="" />
      </div>
      <div class="right">
        <div class="title">
          <img src="@/assets/images/logo.png" alt="" />
          锋团运营平台
        </div>
        <el-form
          :model="loginForm"
          :rules="loginFormRules"
          ref="loginFormRef"
          label-width="auto"
          style="max-width: 380px"
        >
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" :prefix-icon="User" />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              :prefix-icon="Lock"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleLogin">登录</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.login-container {
  width: 100vw;
  height: 100vh;
  background-color: #eee;
  padding: 25px;

  .loginBox {
    display: flex;
    align-items: center;
    width: 100%;
    height: 100%;
    background-color: #fff;
    padding: 0 100px;

    .left {
      flex: 1;
      img {
        width: 100%;
        object-fit: cover;
      }
    }

    .right {
      width: 500px;
      padding: 40px;
      border-radius: 10px;
      box-shadow: 0 2px 12px #0000001a;

      .title {
        font-size: 50px;
        font-weight: 700;
        margin-bottom: 30px;
        img {
          width: 50px;
        }
      }

      .el-input {
        height: 40px;
      }

      .el-button {
        width: 100%;
        height: 40px;
      }
    }
  }
}
</style>
